<template>
  <div class="pageModel">
    <el-dialog
      v-model="centerDialogVisible"
      :title="modelConfig.title ?? '新建用户'"
      width="30%"
      center
      destroy-on-close
    >
      <HyForm v-model="formData" v-bind="modelConfig"></HyForm>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="centerDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="toHandleEditor">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import javaRequest from "@/service/javaInterface.js";
import updateUserInfo from "@/service/main/system/updateUser.js";
import { ElMessage } from "element-plus";
import updateBooks from "@/service/main/system/updateBooks.js";
import HyForm from "@/base-ui/form";
import { ref, watch, computed } from "vue";
import { useStore } from "vuex";
export default {
  name: "pageModel",
  components: { HyForm },
  props: {
    defaultInfo: {
      type: Object,
      default: () => ({}),
    },
    modelConfig: {
      type: Object,
      required: true,
    },
  },
  setup(props) {
    const centerDialogVisible = ref(false);
    const formData = ref({});
    const store = useStore();
    const pageInfo = ref({
      currentPage: 0,
      pageSize: 10,
    });
    watch(
      () => props.defaultInfo,
      (newval) => {
        for (const item of props.modelConfig.formItems) {
          formData.value[`${item.field}`] = newval[`${item.field}`];
        }
      }
    );
    const toHandleEditor = async () => {
      const currentPage = computed(() => store.state.currenPage);
      //判断用户管理那里的
      if (props.modelConfig.name && props.modelConfig.name == "user") {
        const result = await updateUserInfo("/updateUser", formData.value);
        if ((result.data.code == 200) & (result.status == 200)) {
          ElMessage({
            message: result.data.message,
            type: "success",
          });
          store.dispatch("system/getUserPageList", {
            pageUrl: "/getuserList",
            queryInfo: {
              offset: (currentPage.value - 1) * pageInfo.value.pageSize,
              size: pageInfo.value.pageSize,
            },
          });
          centerDialogVisible.value = false;
        }
        return;
      }
      //判断是图书管理那里的
      const result = await updateBooks("/books", formData.value);
      if (result.data.flag) {
        ElMessage({
          message: "修改成功",
          type: "success",
        });
        setTimeout(() => {
          centerDialogVisible.value = false;
        }, 500);
        const refreshResult = await javaRequest.request({
          url: `/books/${currentPage.value}/10`,
          method: "GET",
        });
        if (!refreshResult.data.flag) {
          ElMessage.error("刷新异常");
          return;
        }
        store.commit("Books/changebooksList", refreshResult.data.data.records);
      } else {
        ElMessage.error("修改失败");
        centerDialogVisible.value = false;
      }
    };
    return {
      centerDialogVisible,
      formData,
      toHandleEditor,
    };
  },
};
</script>

<style lang="scss" scope>
.pageModel {
}
</style>
